package com.foucus.myfouces.MVP.Presenler.Presenler_class;

import android.content.Context;
import android.util.Log;

import com.foucus.myfouces.Api.GankService;
import com.foucus.myfouces.Base.BaseReq;
import com.foucus.myfouces.Base.Setting;
import com.foucus.myfouces.MVP.Model.BannerResp;
import com.foucus.myfouces.MVP.Presenler.IPresenler.IBannerP;
import com.foucus.myfouces.MVP.View.IView.IHomePageFG;
import com.foucus.myfouces.Resource.HttpUtil.ServiceFactory;
import com.foucus.myfouces.Resource.Util.GsonImpl;
import com.foucus.myfouces.Resource.Util.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yf-06 on 2017/7/12.
 */

public class BannerP implements IBannerP {
    IHomePageFG iHomePageFG;
    private Context context;
    private List<BannerResp.ResultBean.DataBean> bannerList = new ArrayList<>();

    public BannerP(Context context, IHomePageFG iHomePageFG) {
        this.context = context;
        this.iHomePageFG = iHomePageFG;
    }


    @Override
    public void getBannerList(String type) {
        BaseReq req = new BaseReq();
        req.setValue("type", type);
        ServiceFactory.getInstance()
                .createService(GankService.class)
                .getBannerAd(req.getSecret("getBannerAd"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        Log.e("ssss", "111==>" + bannerList.size());
                        iHomePageFG.onBannerCompleted(bannerList);
                    }

                    @Override
                    public void onError(Throwable e) {
                        iHomePageFG.onBannerError();
                    }

                    @Override
                    public void onNext(ResponseBody argumentResp) {

                        String response = JsonUtil.JsonToStr(argumentResp);
                        boolean isflag = Setting.isRight(context, response);
                        if (isflag) {
                            BannerResp resp = GsonImpl.get().toObject(response, BannerResp.class);
                            bannerList.clear();
                            bannerList.addAll(resp.getResult().getData());

                        } else {
                        }
                    }
                });
    }
}
